#include <iostream>
using namespace std;
int t;
const int N=3e6+10;
int tr[N][65];
int p[N]; 
int idx=0;
int n,q; 
int get_num(char ch)
{
	if(ch>='a'&&ch<='z')return ch-'a';
	else if(ch>='A'&&ch<='Z')return ch-'A'+26;
	else return ch-'0'+52;
}
void insert(string& s)
{
	int cur=0;
	p[cur]++;
	for(auto ch:s)
	{
		int path=get_num(ch);
		if(tr[cur][path]==0)tr[cur][path]=++idx;
		cur=tr[cur][path];
		p[cur]++;
	}
}
int find_pre(string& s)
{
	int cur=0;
	for(auto ch:s)
	{
		int path=get_num(ch);
		if(tr[cur][path]==0)return 0;
		cur=tr[cur][path];
	}
	return p[cur];
}
int main()
{
	
	cin>>t;
	while(t--)
	{
		for(int i=0;i<=idx;i++)
	{
		p[i]=0;
		for(int j=0;j<65;j++)
		{
			tr[i][j]=0;
		}
	}
	idx=0;
		cin>>n>>q;
		
		for(int i=0;i<n;i++)
		{
			string s;cin>>s;
			insert(s);
		}
		while(q--)
		{
			string s;
			cin>>s;
			cout<<find_pre(s)<<endl;
		}
	}
	return 0;
}
